package cn.hznc.controller.permission;

import cn.hznc.aspect.BusinessType;
import cn.hznc.aspect.Log;
import cn.hznc.controller.service.permission.RolesPermissionService;
import cn.hznc.domain.entity.MenuRelationEntity;
import cn.hznc.domain.entity.RolesEntity;
import cn.hznc.domain.request.DynamicMenuRequest;
import cn.hznc.domain.request.permission.RolesPermissionReq;
import cn.hznc.domain.response.PageResult;
import cn.hznc.domain.response.ResponseBody;
import cn.hznc.exception.UserException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping("/rolesPermisson")
public class RolesPermissionController {

    @Resource
    private RolesPermissionService rolesPermissionService;

    /**
     * 根据角色名查询
     * @param pageSize
     * @param pageNo
     * @param rolesName
     * @return
     */
    @RequestMapping(value = "/queryPageByRolesName", method = RequestMethod.GET)
    public ResponseBody<PageResult<RolesEntity>> queryPageByRolesName(@RequestParam Integer pageSize,
                                                                     @RequestParam Integer pageNo,
                                                                     @RequestParam String rolesName) {
        return ResponseBody.success(rolesPermissionService.queryPageByRolesName(pageSize,pageNo,rolesName));
    }

    /**
     * 根据角色名称查询菜单
     * @param rolesId
     * @return
     */
    @RequestMapping(value = "/queryMenuByRolesId", method = RequestMethod.GET)
    public ResponseBody queryMenuByRolesId(@RequestParam Integer rolesId) {
        return ResponseBody.success(rolesPermissionService.queryMenuByRolesId(rolesId));
    }

    /**
     * 根据角色名称查询菜单
     * @param rolesId
     * @return
     */
    @RequestMapping(value = "/queryMenuByRolesId2", method = RequestMethod.GET)
    public ResponseBody queryMenuByRolesId2(@RequestParam Integer rolesId) {
        return ResponseBody.success(rolesPermissionService.queryMenuByRolesId2(rolesId));
    }

    /**
     * 查询全部菜单
     * @return
     */
    @RequestMapping(value = "/queryAllMenu", method = RequestMethod.GET)
    public ResponseBody<List<MenuRelationEntity>> queryAllMenu() {
        return ResponseBody.success(rolesPermissionService.queryAllMenu());
    }

    /**
     * 查询全部菜单
     * @return
     */
    @RequestMapping(value = "/queryAllMenu2", method = RequestMethod.GET)
    public ResponseBody<List<DynamicMenuRequest>> queryAllMenu2() {

        try {
            return ResponseBody.success(rolesPermissionService.queryAllMenu2());
        } catch (Exception ex) {
            return ResponseBody.error("10001", ex.getMessage());
        }
    }

    /**
     * 根据角色名id删除
     * @param rolesId
     * @return
     */
    @RequestMapping(value = "/deleteByRolesId", method = RequestMethod.GET)
    @Log(title = "删除角色" , businessType = BusinessType.DELETE)
    public ResponseBody deleteEmployee(@RequestParam Integer rolesId) {
        return ResponseBody.success(rolesPermissionService.deleteRolesByRolesId(rolesId));
    }

    /**
     * 根据角色名id和菜单id编辑
     * @param rolesPermissionReq
     * @return
     */
    @RequestMapping(value = "/updateRolesByRolesIdAndMenuId", method = RequestMethod.POST)
    @Log(title = "编辑角色对应的菜单" , businessType = BusinessType.UPDATE)
    public ResponseBody updateRolesByRolesIdAndMenuId(@RequestBody RolesPermissionReq rolesPermissionReq) {
        return ResponseBody.success(rolesPermissionService.updateRolesByRolesIdAndMenuId(rolesPermissionReq));
    }

    /**
     * 根据角色名id和菜单id编辑
     * @param rolesPermissionReq
     * @return
     */
    @RequestMapping(value = "/updateRolesByRolesIdAndMenuId2", method = RequestMethod.POST)
    @Log(title = "编辑角色对应的菜单" , businessType = BusinessType.UPDATE)
    public ResponseBody updateRolesByRolesIdAndMenuId2(@RequestBody RolesPermissionReq rolesPermissionReq) {
        return ResponseBody.success(rolesPermissionService.updateRolesByRolesIdAndMenuId2(rolesPermissionReq));
    }

    @RequestMapping(value = "/insertRoles", method = RequestMethod.GET)
    @Log(title = "新增角色" , businessType = BusinessType.INSERT)
    public ResponseBody insertRoles(@RequestParam String rolesName) throws Exception {
        try{
            rolesPermissionService.insertRoles(rolesName);
            return ResponseBody.success();
        }catch (Exception ex){
            return ResponseBody.error(ex.getMessage(),ex.getMessage());
        }
    }
}
